package uiLayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;

import modelLayer.Customer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dialog.ModalityType;

import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.UIManager;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ScrollPaneConstants;

import ctrlLayer.CtrBPartner;

public class UiAddCustomerDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable customerTable;
	private CtrBPartner ctrBp;
	private JTextField txtNoSearch;
	private JTextField txtNameSearch;
	private Customer cus;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UiAddCustomerDialog dialog = new UiAddCustomerDialog(null, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UiAddCustomerDialog(JFrame frame, Customer c) {
		super(frame);
		cus = c;
		setTitle("Tilf\u00F8j kunde");
		ctrBp = new CtrBPartner();
		setModalityType(ModalityType.APPLICATION_MODAL);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int width = 650;
		int height = 350;
		int X = (screen.width / 2) - (width / 2); // Center horizontally.
		int Y = (screen.height / 2) - (height / 2); // Center vertically.
		setBounds(X, Y, width, height);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel searchPanel = new JPanel();
			searchPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Kunde s\u00F8gning", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			contentPanel.add(searchPanel, BorderLayout.NORTH);
			GridBagLayout gbl_searchPanel = new GridBagLayout();
			gbl_searchPanel.columnWidths = new int[]{330, 375, 0};
			gbl_searchPanel.rowHeights = new int[]{0, 0};
			gbl_searchPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
			gbl_searchPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
			searchPanel.setLayout(gbl_searchPanel);
			{
				JPanel leftPanel = new JPanel();
				leftPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
				GridBagConstraints gbc_leftPanel = new GridBagConstraints();
				gbc_leftPanel.fill = GridBagConstraints.BOTH;
				gbc_leftPanel.insets = new Insets(0, 0, 0, 5);
				gbc_leftPanel.gridx = 0;
				gbc_leftPanel.gridy = 0;
				searchPanel.add(leftPanel, gbc_leftPanel);
				leftPanel.setLayout(new GridLayout(2, 2, 0, 5));
				{
					JLabel lblNoSearch = new JLabel("S\u00F8g p\u00E5 kundenr:");
					leftPanel.add(lblNoSearch);
				}
				{
					txtNoSearch = new JTextField();
					txtNoSearch.getDocument().addDocumentListener(new DocumentListener() {

						@Override
						public void removeUpdate(DocumentEvent arg0) {
							if(txtNoSearch.getText().length() > 0){
								txtNameSearch.setEditable(false);
							}
							else{
								txtNameSearch.setEditable(true);
							}
						}

						@Override
						public void insertUpdate(DocumentEvent arg0) {
							if(txtNoSearch.getText().length() > 0){
								txtNameSearch.setEditable(false);
							}
							else{
								txtNameSearch.setEditable(true);
							}
						}

						@Override
						public void changedUpdate(DocumentEvent arg0) {
							if(txtNoSearch.getText().length() > 0){
								txtNameSearch.setEditable(false);
							}
							else{
								txtNameSearch.setEditable(true);
							}
						}
					});
					txtNoSearch.setColumns(10);
					leftPanel.add(txtNoSearch);
				}
				{
					JLabel lblNameSearch = new JLabel("S\u00F8g p\u00E5 kundenavn:");
					leftPanel.add(lblNameSearch);
				}
				{
					txtNameSearch = new JTextField();
					txtNameSearch.getDocument().addDocumentListener(new DocumentListener() {

						@Override
						public void removeUpdate(DocumentEvent arg0) {
							if(txtNameSearch.getText().length() > 0){
								txtNoSearch.setEditable(false);
							}
							else{
								txtNoSearch.setEditable(true);
							}
						}

						@Override
						public void insertUpdate(DocumentEvent arg0) {
							if(txtNameSearch.getText().length() > 0){
								txtNoSearch.setEditable(false);
							}
							else{
								txtNoSearch.setEditable(true);
							}
						}

						@Override
						public void changedUpdate(DocumentEvent arg0) {
							if(txtNameSearch.getText().length() > 0){
								txtNoSearch.setEditable(false);
							}
							else{
								txtNoSearch.setEditable(true);
							}
						}
					});
					txtNameSearch.setColumns(10);
					leftPanel.add(txtNameSearch);
					
				}
			}
			{
				JPanel rightPanel = new JPanel();
				GridBagConstraints gbc_rightPanel = new GridBagConstraints();
				gbc_rightPanel.fill = GridBagConstraints.HORIZONTAL;
				gbc_rightPanel.gridx = 1;
				gbc_rightPanel.gridy = 0;
				searchPanel.add(rightPanel, gbc_rightPanel);
				{
					JButton btnSearch = new JButton("S\u00F8g");
					btnSearch.setPreferredSize(new Dimension(100, 25));
					btnSearch.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent arg0) {
							//findCustomer();
						}
					});
					rightPanel.add(btnSearch);
				}
				{
					JButton btnListAll = new JButton("Vis alle");
					btnListAll.setPreferredSize(new Dimension(100, 25));
					btnListAll.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent e) {
							//listAll();
						}
					});
					rightPanel.add(btnListAll);
				}
			}
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			contentPanel.add(scrollPane, BorderLayout.CENTER);
			{
				customerTable = new UiTables().customerTable(ctrBp.retrieveAllCustomers());
				scrollPane.setViewportView(customerTable);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Tilf\u00F8j");
				okButton.setPreferredSize(new Dimension(100, 25));
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						getSelectedCustomer();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Annuller");
				cancelButton.setPreferredSize(new Dimension(100, 25));
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						cancelAction();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	protected void cancelAction() {
		super.setVisible(false);
		super.dispose();
	}

	public void getSelectedCustomer() {
		cus.setPartnerId(ctrBp.retrieveCustomer(Integer.parseInt(customerTable.getValueAt(customerTable.getSelectedRow(), 0).toString())).getPartnerId());
		//System.out.println(orderCtr.getOrder().getOrderNo());
		super.setVisible(false);
	}

	/*
	private void findCustomer() {
		String customerName = txtNameSearch.getText();
		ArrayList<Customer> searchResult = new ArrayList<>();

		if (!txtNoSearch.getText().isEmpty()){
			int number = Integer.parseInt(txtNoSearch.getText());
			try {

				searchResult.add(customerCtr.getCustomer(number));
			}
			catch (IllegalArgumentException e) {
				showErrorMessage(e.getMessage());
			}

		} else if (customerName != null) {
			try {
				searchResult = customerCtr.getCustomerByName(customerName);
			}
			catch (IllegalArgumentException e) {
				showErrorMessage(e.getMessage());
			}
		}
		updateTable(searchResult);
		clearTextFields();
	}

	private void listAll(){
		//List<Customer> customers = new CustomerCtr().getAll();
		List<Customer> customers = customerCtr.getAll();
		updateTable(customers);
//		if (!(customers.size() > 0)){
//			JOptionPane.showMessageDialog(this, "Der er ingen kunder på listen.", "Tom liste", JOptionPane.OK_OPTION);
//		}
	}

	private void updateTable(List<Customer> customers){
		((CustomTableIF<Customer>)customerTable).setModel(customers);
	}


	private void clearTextFields() {
		txtNameSearch.setText("");
		txtNoSearch.setText("");
	}

	private void showErrorMessage(String message) {

		JOptionPane.showMessageDialog(this, 
				message, 
				"Error 40", 
				JOptionPane.OK_OPTION);

	}
*/

}
